package server

import (
	"4-7zk/common/model/mysql"
	"4-7zk/common/model/redis"
	"4-7zk/common/proto/user"
	"context"
	"errors"
	"fmt"
	jwt "github.com/golangblogs/gojwt"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func Login(req *user.LoginReq) (*user.LoginResp, error) {
	getUser, err := mysql.GetUser(req.Phone)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	if getUser.Phone == "" {
		m := mysql.User{
			Model:    gorm.Model{},
			Phone:    req.Phone,
			Password: req.Password,
		}
		err := mysql.Create(&m)
		if err != nil {
			return nil, errors.New("注册失败")
		}
	}
	if getUser.Password != req.Password {
		return nil, errors.New("密码错误")
	}
	claims := jwt.CustomClaims{ID: getUser.ID}
	token, err := jwt.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		return nil, errors.New("token生成失败")
	}
	return &user.LoginResp{Greet: token}, nil
}

func CourseAdd(req *user.CourseShowReq) (*user.CourseShowResp, error) {
	show, err := mysql.CourseShow(int(req.Id))
	if err != nil {
		return nil, errors.New("查询失败")
	}
	return &user.CourseShowResp{
		UserId:      int64(show.UserId),
		CourseName:  show.CourseName,
		CoursePrice: float32(show.CoursePrice),
		CourseInter: show.CourseInter,
		CourseTime:  show.CourseTime,
		CourseCoach: show.CourseCoach,
	}, nil
}

func Reservation(req *user.ReservationReq) (*user.ReservationResp, error) {
	show, err := mysql.CourseShow(int(req.UserId))
	if err != nil {
		return nil, errors.New("查询失败")
	}
	m := map[string]interface{}{
		"UserId":          show.ID,
		"CourseName":      show.CourseName,
		"CoursePrice":     show.CoursePrice,
		"CourseInter":     show.CourseInter,
		"CourseTime":      show.CourseTime,
		"CourseCoach":     show.CourseCoach,
		"CourseId":        req.CourseId,
		"CourseData":      req.CourseData,
		"CoursePeoPleNum": req.CoursePeoPleNum,
		"ReservationNum":  req.ReservationNum,
		"Stadium":         req.Stadium,
		"Address":         req.Address,
		"Status":          1,
	}
	sprintf := fmt.Sprintf("user:%d:course%d", req.UserId, req.CourseId)
	val := redis.Rdb.Set(context.Background(), sprintf, m, time.Minute*30).Val()
	return &user.ReservationResp{Success: val}, nil
}
func ReservationAdd(req *user.ReservationAddReq) (*user.ReservationAddResp, error) {
	show, err := mysql.CourseShow(int(req.CourseId))
	if err != nil {
		return nil, errors.New("查询失败")
	}
	res := mysql.ReservationCouRes{
		Model:           gorm.Model{},
		UserId:          show.UserId,
		CourseId:        int(show.ID),
		CourseName:      show.CourseName,
		CoursePrice:     show.CoursePrice,
		CourseTime:      show.CourseTime,
		CourseCoach:     show.CourseCoach,
		CourseData:      req.CourseData,
		CourseStaData:   time.Now(),
		CoursePeoPleNum: int(req.CoursePeoPleNum),
		ReservationNum:  int(req.ReservationNum),
		Stadium:         req.Stadium,
		Address:         req.Address,
		Notes:           req.Notes,
		Status:          1,
	}
	err = mysql.ReservationCreate(&res)
	if err != nil {
		return nil, errors.New("预约失败")
	}
	return &user.ReservationAddResp{Success: "预约成功"}, nil
}

func ReservationList(req *user.ReservationListReq) (*user.ReservationListResp, error) {
	list, err := mysql.List(int(req.Status))
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var lists []*user.List
	for _, r := range list {
		lists = append(lists, &user.List{
			UserId:          int64(r.UserId),
			CourseName:      r.CourseName,
			CoursePrice:     float32(r.CoursePrice),
			CourseInter:     strconv.Itoa(r.CourseId),
			CourseTime:      r.CourseTime,
			CourseCoach:     r.CourseCoach,
			CourseId:        int64(r.CourseId),
			CourseData:      r.CourseData,
			CoursePeoPleNum: int64(r.CoursePeoPleNum),
			ReservationNum:  int64(r.ReservationNum),
			Stadium:         r.Stadium,
			Address:         r.Address,
			Notes:           r.Notes,
			Status:          int64(r.Status),
		})
	}
	return &user.ReservationListResp{List: lists}, nil
}

func ReservationUpdate(req *user.ReservationUpdateReq) (*user.ReservationUpdateResp, error) {
	val := redis.Rdb.Set(context.Background(), "id"+strconv.FormatInt(req.Id, 10), "还有30分钟开始课程，请注意", time.Minute*30).Val()
	return &user.ReservationUpdateResp{Success: val}, nil
}
